<?php

namespace App\Http\Controllers;

use App\Common\HttpClient;
use App\Common\Logger;
use App\Common\Sql;
use App\Common\Tool;

class Login extends Base
{
    public function logic()
    {
        if (!isset($this->param['user'])) {
            return $this->returnError(1, "手机号输入不能为空");
        }
        if (!isset($this->param['password'])) {
            return $this->returnError(2, "密码输入不能为空");
        }

        $login_ip = $_SERVER["REMOTE_ADDR"];
        $user     = strtolower($this->param['user']);
        $row      = Sql::select('t_player_info', [['phone_num', $user]]);
        if (count($row) == 0) {
            return $this->returnError(3, '您输入的手机账户不存在');
        } elseif (count($row) > 1) {
            Logger::instance()->error(Tool::getUri(),"{$user} repeat");
            return $this->returnError(4, "错误, 出现重复账户");
        }
        $passwd_md5 = md5($this->param['password']);
        if ($passwd_md5 != $row[0]['passwd_md5']) {
            return $this->returnError(5, '密码错误');
        }
        if ($row[0]['status'] == 0) {
            return $this->returnError(6, "对不起，您的账号被管理员禁号，如有疑问请联系客户");
        }

        $uid      = $row[0]['id'];
        $skey     = md5(time());

        $province   = "未知";
        $city       = "未知";
        $url_prefix = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=";
        $url        = $url_prefix . $login_ip;

        $client     = new HttpClient();
        $ret        = $client->Get($url);
        if ($ret == false) {
        } else {
            $find = (array)json_decode($ret, true);
            if (!empty($find["ret"]) && $find['ret'] == 1) {
                $province = $find["province"];
                $city     = $find["city"];
            }
        }

        Sql::update('t_player_info', [['id', $uid]], ['login_ip' => $login_ip, 'skey' => $skey]);
        Sql::insert('t_login_rec', [
            "login_uid" => $uid,
            'login_type' => 1,
            'login_ip' => $login_ip,
            'login_province' => $province,
            'login_city' => $city,
        ]);
        $user_info['uid']          = (int)$uid;
        $user_info['nick_name']    = $row[0]['nick_name'];
        $user_info['sex']          = (int)$row[0]['sex'];
        $user_info['avater_url']   = $row[0]['avater_url'];
//        $user_info['room_ticket']  = (int)$row[0]['room_ticket'];
        $user_info['gold']         = (int)$row[0]['gold'];
        $user_info['diamonds']     = (int)$row[0]['diamonds'];
        $user_info['login_ip']     = $login_ip;
//        $user_info['win_board']    = (int)$row[0]['win_board'];
//        $user_info['lose_board']   = (int)$row[0]['lose_board'];
//        $user_info['pin_board']    = (int)$row[0]['pin_board'];
        $user_info['skey']         = $skey;
        $user_info['paiwei_score'] = (int)$row[0]['paiwei_score'];

        $ret_arr                       = $this->cal_paiwei_rank($user_info['paiwei_score']);
        $user_info['paiwei_rank']      = $ret_arr["paiwei_rank"];
        $user_info['paiwei_rank_name'] = $ret_arr["paiwei_rank_name"];

        //每日登陆任务
        $this->add_task_unclaimed_num((int)$uid, 2);

        return $this->returnData($user_info);

    }

    private function cal_paiwei_rank($paiwei_score)
    {
        $row = Sql::select('t_rank_conf', [['min_score', '<=', $paiwei_score], ['max_score', '>', $paiwei_score]]);
        if (count($row) <= 0) {
            Logger::instance()->error(Tool::getUri(),"get paiwei error.\n");
            $rank                     = array();
            $rank['paiwei_rank']      = 0;
            $rank['paiwei_rank_name'] = "无";
            return $rank;
        }
        $rank                     = array();
        $rank['paiwei_rank']      = (int)$row[0]["id"];
        $rank['paiwei_rank_name'] = $row[0]["name"];
        return $rank;
    }

}